package view;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.StuinfoDao;
import entity.StuInfo;

public class SelectShow extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField dangechaxuntf;
	private JTextField[] showtf = new JTextField[3];
	private StuInfo stuInfo;
	private StuinfoDao stuinfoDao;


	public SelectShow() {
		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());

		String[] sebt = { "单个查询", "全部查询" };
		String[] selb = { "根据学号查询单个学生信息", "查询全部学生信息" };
		String[] str = { "学号", "姓名", "性别" };
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		dangechaxuntf = new JTextField(5);
		gbc.gridx = 2;
		gbc.gridy = 0;
		pan.add(dangechaxuntf, gbc);
		for (int i = 0; i < sebt.length; i++) {
			JLabel lb = new JLabel(selb[i]);
			gbc.gridx = 0;
			gbc.gridy = i;
			pan.add(lb, gbc);
			
			JButton jb = new JButton(sebt[i]);
			gbc.gridx = 3;
			gbc.gridy = i;
			gbc.insets = new Insets(20, 0, 20, 0);
			pan.add(jb, gbc);
			jb.addActionListener(this);
		}

		for (int i = 0; i < str.length; i++) {
			JLabel la = new JLabel(str[i]);
			gbc.gridx = 0;
			gbc.gridy = 6 + i;
			pan.add(la, gbc);
			gbc.insets = new Insets(20, 10, 20, 10);

			showtf[i] = new JTextField(20);
			gbc.gridx = 2;
			gbc.gridy = 6+ i;
			showtf[i].setEnabled(false);
			showtf[i].setDisabledTextColor(Color.BLACK);
			pan.add(showtf[i], gbc);

		}

    	add(pan);
   
   
	}

	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd=e.getActionCommand();
		switch (cmd) {
		case "单个查询":
		 	stuInfo=new StuInfo();
			stuinfoDao=new StuinfoDao();
			stuInfo.setId(Integer.valueOf(dangechaxuntf.getText()));
			stuInfo=stuinfoDao.getOne(stuInfo);
			showtf[0].setText(String.valueOf(stuInfo.getId()));
			showtf[1].setText(stuInfo.getName());
			showtf[2].setText(stuInfo.getSex());
			break;
    
		}
		
	}

	
}
